package com.kylin.common.weixin.miniapp;

import com.alibaba.fastjson2.JSONObject;
import com.kylin.common.weixin.util.WxRequest;
import org.springframework.util.StringUtils;

/**
 * 消息工具类
 * @author wuhao
 * @version 1.0 - 2023/8/30
 */
public class MessageService {

    /** 下发统一消息 */
    private static final String UNIFORM_MESSAGE_URL = "/cgi-bin/message/wxopen/template/uniform_send";
    /** 发送订阅消息 */
    private static final String SUBSCRIBE_MESSAGE_URL = "/cgi-bin/message/subscribe/send";

    /**
     * 下发统一消息
     * @param accessToken
     * @param data 模板消息对象
     * @return
     * @author wuhao
     * @version 1.0.0 - 2023/8/30
     */
    public static JSONObject sendUniformMessage(String accessToken, JSONObject data){
        String url = UNIFORM_MESSAGE_URL;
        if(StringUtils.hasText(accessToken)) {
            url += "?access_token=" + accessToken;
        }
        return WxRequest.post(url,data.toJSONString());
    }

    /**
     * 发送订阅消息
     * @param accessToken
     * @param data 模板消息对象
     * @return
     * @author wuhao
     * @version 1.0.0 - 2023/9/1
     */
    public static JSONObject sendSubscribe(String accessToken, JSONObject data) {
        String url = SUBSCRIBE_MESSAGE_URL;
        if(StringUtils.hasText(accessToken)) {
            url += "?access_token=" + accessToken;
        }
        return WxRequest.post(url,data.toJSONString());
    }

}
